<?php
/**
 * Created by PhpStorm.
 * User: 何婷婷
 * Date: 2016/11/8
 * Time: 14:22
 */

namespace App\Core;

use App\Model\UserModel;


abstract class Model
{
    protected $db;
    protected $id;
    public $data = array();  //存储从数据库调出的数据

    public function __construct($table)
    {
        $this->db = new DB();
        $this->db->setTable($table);
    }


    //TODO:save to database
    public function save($data = '', $name = "")
    {
        if (!is_array($data))
            $data = $this->data;
        $result = $this->db->setWhere(array('name' => $name))->select();
        var_dump($data);
        if ($result) {
            $this->id = $result['id'];
            if (isset($id)) {
                return $this->db->update($data, array('id' => $id));
            }
        } else
            return $this->db->insert($data);
    }

    //获取数据库id
    /*  public function getId($name)
      {
          $result = $this->db->setWhere(array('name' => $this->$name))->select();
          var_dump($result);
          if ($result) {
              return $this->id = $result['id'];
          } else
              return false;
      }*/

    //获取user
    /*public function getById($id)
    {
        $result = $this->db->setWhere(array('id' => $id))->select();
        if ($result) {
            $this->data = $result;
            return $this->data;
        } else
            return false;

    }*/

    //更新数据
    public function updateUserInfo($data, $where)
    {
        return $this->db->setWhere($where)->update($data);
    }


    //插入数据
    public function insertUserInfo($data)
    {
        return $this->db->insert($data);
    }


    //列出用户数据
    public function listTable($name)
    {
        $result = $this->db->setWhere(array('name' => $name))->select();
        if ($result) {
            $this->data = $result;
            return $this->data;
        } else
            return false;
    }

    //删除数据
    public function delete($where)
    {
        return $this->db->delete($where);
    }


}

